package com.sec.samsung.android.app.baymax.view.adapter;

import java.util.ArrayList;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sec.samsung.android.app.baymax.R;
import com.sec.samsung.android.app.baymax.model.user.Account;
import com.sec.samsung.android.app.baymax.utils.BaymaxUtils;
import com.sec.samsung.android.app.baymax.utils.StringUtility;
import com.sec.samsung.android.app.baymax.view.undo.SwipeDismissTouchListener;

public class ListHistoryAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Account> mUserList;
    private HistoryDeleteListener listener;

    public ListHistoryAdapter(Context context, ArrayList<Account> userList) {
        this.mContext = context;
        this.mUserList = userList;
    }

    @Override
    public int getCount() {
        return mUserList.size();
    }

    @Override
    public Account getItem(int position) {
        return this.mUserList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        if (convertView == null) {
            convertView = (RelativeLayout) RelativeLayout.inflate(mContext, R.layout.history_item, null);
        }
        final ViewHolder holder = new ViewHolder();
        holder.imgPhoto = (ImageView) convertView.findViewById(R.id.fb_friend_avatar);
        holder.txtName = (TextView) convertView.findViewById(R.id.fb_friend_name);
        holder.layoutInfo = (RelativeLayout) convertView.findViewById(R.id.layout_info);
        holder.linearDelete = (LinearLayout) convertView.findViewById(R.id.layout_delete);

        holder.txtName.setSelected(true);
        holder.txtName.setText(mUserList.get(position).getName());

        holder.imgPhoto.setFocusable(false);
        Log.d("KieuThang", "mUserList.get(position).mUserAvatarData:" + mUserList.get(position).getAvatarData());
        if (!StringUtility.isEmpty(mUserList.get(position).getAvatarData()) && !"null".equals(mUserList.get(position).getAvatarData()))
            holder.imgPhoto.setImageBitmap(BaymaxUtils.convertBitmapFromString(mUserList.get(position).getAvatarData()));
        else
            holder.imgPhoto.setImageResource(R.drawable.s_health_profilesetting_male_l_icon_dim);

        holder.imgPhoto.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.onClickItem(position);
            }
        });
        holder.txtName.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.onClickItem(position);
            }
        });
        holder.linearDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                holder.layoutInfo.animate().translationX(0).alpha(1).setDuration(500).setListener(new AnimatorListener() {

                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        listener.deleteHistory(position);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }
                });
            }
        });
        holder.layoutInfo.setOnTouchListener(new SwipeDismissTouchListener(mContext, holder.layoutInfo, null,
                new SwipeDismissTouchListener.OnDismissCallback() {
                    public void onEndAnimation(View view, Object token) {
                    }

                    @Override
                    public void onStartAnimation(boolean isMoveRight) {
                    }
                }));
        holder.layoutInfo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.onClickItem(position);
            }
        });
        return convertView;
    }

    class ViewHolder {
        RelativeLayout layoutInfo;
        ImageView imgPhoto;
        TextView txtName;
        LinearLayout linearDelete;
    }

    public interface HistoryDeleteListener {
        public void deleteHistory(int position);

        public void onClickItem(int position);
    }

    public void setListener(HistoryDeleteListener listener) {
        this.listener = listener;
    }
}
